#!/bin/sh
#                      setup
#                     -------------
# Description:
#   Link files into a local src directory
#
#
IAM=$0
#
link_scripts()
{
  echo "Linking job submission/execution scripts."
  to_link=
  for script in $scripts
  do
    if [ -f $bindir/$script ]; then
      [ -h ./$script ] && rm -f $script
      [ ! -f ./$script ] && to_link="$to_link $bindir/$script"
    else
      echo "No such file: $bindir/$script"
    fi
  done
  [ "$to_link" ] && ln -s $to_link .
}
#
link_root_makefile()
{
## Make a link to a root Makefile to allow make from the run directory
  [ -h Makefile ] && rm -f Makefile
  [ ! -f Makefile ] && ln -s $srcdir/Makefile.parent Makefile
  ln -s $srcdir/Makefile.parent Makefile
  echo Linked root Makefile.
}
#
#-----------------------------------------------------------------------------
make_src_dir()
{
## src subdirectory
  if [ ! -d $src ]; then
    echo "No src directory found: creating new one (`pwd`/src)."
    mkdir $src
  fi
}
#
link_src_files()
{
#
#  LINK all *.f90, *.f, and the Makefile to local src directory
#
  echo "Linking files in '$src'."
  old_dir="`pwd`"
  cd $src
  n=0
  to_link=
  for file in $srcdir/*.f90 $srcdir/*.f $srcdir/*.c \
              $srcdir/Makefile.src $srcdir/Makefile.depend \
              $srcdir/Makefile.library $srcdir/Makefile.machine \
              $srcdir/*.h $srcdir/scripts $srcdir/.cvsignore
  do
    if [ -f $file ] || [ -d $file ]; then
      if [ -h "`basename $file`" ]; then
        rm -f "`basename $file`"
        to_link="$to_link $file"
        #ln -s $file .
      elif [ -f "`basename $file`" ] \
        || [ -d "`basename $file`" ]; then
        n="`expr $n + 1`"
        existf="$existf`basename $file` "
      else
        to_link="$to_link $file"
        #ln -s $file .
      fi
    else
      echo "No such file: $file"
    fi
  done
  [ "$to_link" ] && ln -s $to_link .
  cd $old_dir
}
#
#-----------------------------------------------------------------------------
scriptdir=$AMD_HOME/script
srcdir=$AMD_HOME/src
src=./src
#
echo "Files will be linked to the root code directory '$AMD_HOME'."
#
link_scripts
link_root_makefile
make_src_dir
link_src_files
